﻿using System;
using System.Web.Mvc;
using sBlog.Net.Domain.Entities;
using sBlog.Net.Domain.Interfaces;

namespace sBlog.Net.Areas.Admin.Controllers
{
    public class StatusAdminController : Controller
    {
        private readonly IStatus _statusRepository;

        public StatusAdminController(IStatus statusRepository)
        {
            _statusRepository = statusRepository;
        }

        //
        // GET: /Status/
        [Authorize]
        public ActionResult ManageStatus()
        {
            return View(_statusRepository.GetStatusByUserID(GetUserId()));
        }

        //
        // GET: /Status/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Status/Create

        [HttpPost]
        public ActionResult Create(StatusEntity statusEntity)
        {
            try
            {
                // TODO: Add insert logic here
                statusEntity.OwnerUserID = GetUserId();
                statusEntity.CreateDate = DateTime.Now;
                statusEntity.UpdateDate = DateTime.Now;
                _statusRepository.AddStatus(statusEntity);
                return RedirectToAction("ManageStatus");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Status/Edit/5

        public ActionResult Edit(int id)
        {
            return View(_statusRepository.GetStatusByStatusID(id));
        }

        //
        // POST: /Status/Edit/5

        [HttpPost]
        public ActionResult Edit(StatusEntity statusEntity)
        {
            try
            {
                statusEntity.UpdateDate = DateTime.Now;
                _statusRepository.UpdateStatus(statusEntity);

                return RedirectToAction("ManageStatus");
            }
            catch
            {
                return View();
            }
        }

        //
        // POST: /Status/Delete/5
        public ActionResult Delete(int id)
        {
            try
            {
                _statusRepository.DeleteStatusByStatusID(id);
                return RedirectToAction("ManageStatus");
            }
            catch
            {
                return View();
            }
        }

        protected int GetUserId()
        {
            var userId = -1;
            if (Request.IsAuthenticated)
            {
                var userIdentity = (IUserInfo)User.Identity;
                userId = Int32.Parse(userIdentity.UserId);
            }
            return userId;
        }
    }
}
